package com.zhaoguang.car.service.impl;

import com.zhaoguang.car.entity.UserCollect;
import com.zhaoguang.car.mapper.CarMapper;
import com.zhaoguang.car.mapper.UserRelativeMapper;
import com.zhaoguang.car.service.UserRelativeService;
import com.zhaoguang.car.vo.ListCarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserRelativeServiceImpl implements UserRelativeService {

    @Autowired
    UserRelativeMapper userRelativeMapper;

    @Autowired
    CarMapper carMapper;

    @Override
    public String findCarIfCollection(UserCollect uc) {
        String str = userRelativeMapper.findCarIfCollection(uc);
        if (str != null) {
            if (str.equals("true")) {
                return "true";
            }
            return "false";
        }

        return "false";
    }

    @Override
    public int saveCollection(UserCollect uc) {
        uc.setCollect("true");
        int result = userRelativeMapper.saveCollection(uc);
        return result;
    }

    @Override
    public int deleteCollection(UserCollect uc) {
        int result = userRelativeMapper.deleteCollection(uc);

        return 0;
    }

    @Override
    public List<ListCarVo> findUserCollection(int userId) {
        List<Integer> list = userRelativeMapper.findUserCollection(userId);
        List<ListCarVo> listCollection = new ArrayList<>();
        for (Integer i : list) {
            listCollection.add(userRelativeMapper.findAllCarsById(i));
        }
        return listCollection;
    }

}
